// VideoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Define.h"
#include "VaxVoipSample.h"
#include "VideoDlg.h"
#include "afxdialogex.h"
#include "DrawFrame.h"
#include "VaxSIPUserAgent.h"

// CVideoDlg dialog

IMPLEMENT_DYNAMIC(CVideoDlg, CPropertyPage)

CVideoDlg::CVideoDlg(CVaxSIPUserAgent* pVaxSIPUserAgent) : CPropertyPage(CVideoDlg::IDD)
{
	m_pVaxSIPUserAgent = pVaxSIPUserAgent;

	m_objFont.CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Verdana");
	m_bCheckPreview = TRUE;
}

CVideoDlg::~CVideoDlg()
{
	m_objDrawFramePreview.CloseDevice();
	
	m_objScreens[0].CloseDevice();
	m_objScreens[1].CloseDevice();
	m_objScreens[2].CloseDevice();
	m_objScreens[3].CloseDevice();
	m_objScreens[4].CloseDevice();
}

void CVideoDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);

	GetDlgItem(IDC_STATIC_PREVIEW)->SetFont(&m_objFont);
	GetDlgItem(IDC_STATIC_SCREEN1)->SetFont(&m_objFont);
	GetDlgItem(IDC_STATIC_SCREEN2)->SetFont(&m_objFont);
	GetDlgItem(IDC_STATIC_SCREEN3)->SetFont(&m_objFont);
	GetDlgItem(IDC_STATIC_SCREEN4)->SetFont(&m_objFont);
	GetDlgItem(IDC_STATIC_SCREEN5)->SetFont(&m_objFont);

	DDX_Check(pDX, IDC_CHECK_VIDEO_PREVIEW_ACTIVATE, m_bCheckPreview);
}


BEGIN_MESSAGE_MAP(CVideoDlg, CPropertyPage)
	ON_BN_CLICKED(IDC_CHECK_VIDEO_PREVIEW_ACTIVATE, &CVideoDlg::OnBnClickedCheckVideoPreviewActivate)
END_MESSAGE_MAP()

// CVideoDlg message handlers


BOOL CVideoDlg::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	CWnd* pScreenWnd = GetDlgItem(IDC_SCREEN1);
	m_hWndScreen[0] = pScreenWnd->m_hWnd;
	
	pScreenWnd = GetDlgItem(IDC_SCREEN2);
	m_hWndScreen[1] = pScreenWnd->m_hWnd;

	pScreenWnd = GetDlgItem(IDC_SCREEN3);
	m_hWndScreen[2] = pScreenWnd->m_hWnd;

	pScreenWnd = GetDlgItem(IDC_SCREEN4);
	m_hWndScreen[3] = pScreenWnd->m_hWnd;

	pScreenWnd = GetDlgItem(IDC_SCREEN5);
	m_hWndScreen[4] = pScreenWnd->m_hWnd;
	
	/////////////////////////////////////////////////////////////////////////
	
	pScreenWnd = GetDlgItem(IDC_VIDEO_PREVIEW);
	m_hWndPreview = pScreenWnd->m_hWnd;

	m_objDrawFramePreview.OpenDevice(m_hWndPreview);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

void CVideoDlg::OnVaxInitialize()
{
	UpdateData(TRUE);
	m_pVaxSIPUserAgent->ActivateVideoDevice(m_bCheckPreview);
}

void CVideoDlg::OnVaxUnInitialize()
{
	m_pVaxSIPUserAgent->CloseVideoDev();
}

///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

void CVideoDlg::OnVideoDeviceShowRGB(int nDeviceId, BYTE* pFrameRGB, DWORD dwFrameSize, DWORD dwFrameWidth, DWORD dwFrameHeight)
{
	m_objDrawFramePreview.PostData(dwFrameWidth, dwFrameHeight, pFrameRGB);
}

///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

void CVideoDlg::OnVideoRemoteShowStart(int nLineNo)
{
	m_objScreens[nLineNo].OpenDevice(m_hWndScreen[nLineNo]);
}

void CVideoDlg::OnVideoRemoteShowStop(int nLineNo)
{
	m_objScreens[nLineNo].CloseDevice();
}

void CVideoDlg::OnVideoRemoteShowRGB(int nLineNo, BYTE* pFrameRGB, DWORD dwFrameSize, DWORD dwFrameWidth, DWORD dwFrameHeight)
{
	m_objScreens[nLineNo].PostData(dwFrameWidth, dwFrameHeight, pFrameRGB);
}

///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

void CVideoDlg::OnBnClickedCheckVideoPreviewActivate()
{
	UpdateData(TRUE);

	m_pVaxSIPUserAgent->ActivateVideoDevice(m_bCheckPreview);
}
